package com.mark.anto.bootstrap;

import com.mark.anto.condition.OnSystemPropertyCondition;
import com.mark.anto.condition.SystemPropertyCondition;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Profile;
import org.springframework.util.Assert;

/**
 * @Description: Spring条件装配 基于编程方式
 * @Author: Mark
 * @CreateDate: 2019/9/1 9:32
 * @Version: 3.00.001
 * @Copyright : 豆浆油条个人非正式工作室
 *
 * <pre>
 * 定义：Bean装配前的判断
 * 实现：一：基于注解方式 {@link Profile} @since 3.1
 *           举例： {@link ConditionByCodeBootstrap}
 *
 *       二：基于编程方式；自定义{@link Conditional} @since 4.0
 *          举例：{@link ConditionByProfileAnnoBootstrap}
 * <pre/>
 *
 * @see SystemPropertyCondition 自定义的条件注解
 * @see OnSystemPropertyCondition 条件注解是否生效的逻辑实现
 *
 */
public class ConditionByCodeBootstrap {

    @Bean
    @SystemPropertyCondition(name = "user.name", value = "Administrator")
    public String user() {
        return "Hello,World 小马哥";
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionByCodeBootstrap.class)
                .web(WebApplicationType.NONE)
                .run(args);

        // 通过名称和类型获取 helloWorld Bean
        String userInfo = context.getBean("user", String.class);
        Assert.notNull(userInfo, "获取到Bean为为空！");

        // 关闭上下文
        context.close();
    }

}
